<?php
namespace Model;

use Core\Constant;
use Core\Session\AdminSessionHelper;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Predicate\Like;
use Zend\Db\Sql\Predicate\Operator;
use Zend\Db\Sql\Select;
use Zend\InputFilter\InputFilter;
use Zend\Log\Logger;

class PageTable extends AbstractTable
{

    /**
     * @param $id
     * @return Page
     */
    public function getPage($id){
       return $this->getById($id);
    }

    /**
     * @param $name
     * @return Page
     */
    public function getPageByName($name)
    {
       return $this->getByColumn('name', $name)->current();
    }


    public function search($isPaging, $offset, $rowPerPage, $orderArr = array('created_date'), $searchOption){

        $select = $this->getDbSqlSelect();

        $select->join(array('r'=>'route'),'r.page_id = page.id', array('route'=>'route'))
            ->join(array('t'=>'template'),'t.id = page.template_id', array('template'=>'name'));
        if($searchOption['name']){
            $select->where(array('name like "%'.$searchOption['name'].'%"'));
        }

        if($searchOption['module'] && $searchOption['module'] != Constant::$SELECT_ALL_VALUE){
            $select->where(array('module'=>$searchOption['module']));
        }

        if($searchOption['controller']&& $searchOption['controller'] != Constant::$SELECT_ALL_VALUE){
            $select->where(array('controller'=>$searchOption['controller']));
        }

        if($searchOption['template']&& $searchOption['template'] != Constant::$SELECT_ALL_VALUE){
            $select->where(array('template'=>$searchOption['template']));
        }

        if($isPaging){
            $select->offset($offset)->limit($rowPerPage);
        }

        if($orderArr){
            $select->order($orderArr);
        }

//        \Logger::debug($select->getSqlString($this->tableGateway->adapter->getPlatform()));

        $rows = $this->tableGateway->selectWith($select);
        $result['rows'] = $rows;

        $total = $this->getCount($select);

        $result['total'] = $total;

        return $result;

    }

    public function getAllActivePage()
    {
        $select = $this->getDbSqlSelect();
        $select->join(array('r'=>'route'), 'r.page_id = page.id', array('myroute'=>'route'))
            ->where(array('active'=>1))
        ->where(array('r.lang'=> AdminSessionHelper::getCurrentLang()));
        return $this->getTableGateway()->selectWith($select);
    }


    public function getAllPagesByTemplateId($templateId)
    {
        $select = $this->getDbSqlSelect();
        $select = $select->where(array('template_id'=> $templateId));
        return $this->getTableGateway()->selectWith($select);
    }


    /**
     * @param $url
     * @return null | Page
     */
    public function getPageByRoute($url)
    {
        $select = $this->getDbSqlSelect();
        $select->join(array('r'=>'route'), 'r.page_id = page.id', array('myroute'=>'route'))
            ->where(array('page.active'=>1))
            ->where(array('r.lang'=> AdminSessionHelper::getCurrentLang()))
            ->where(array('r.route'=>$url));
        $rsi = $this->getTableGateway()->selectWith($select);
        foreach ($rsi as $aPage) {
            return $aPage;
        }
    }
}